package com.yupi.project.handler;

import com.yupi.project.model.AuthenticatorResult;
import com.yupi.project.model.ProtectedRequest;
import com.yupi.project.model.RequestHolder;
import com.yupi.project.model.RequestInfo;
import com.yupi.project.security.SecurityVerificationHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @description: 剩余积分校验
 * @author SummerBird
 * @date 2024/3/8 19:43
 * @version 1.0
 */
@Component
@Slf4j
public class CheckWoolLeftHandler implements SecurityVerificationHandler {
    @Override
    public AuthenticatorResult auth(ProtectedRequest protectedRequest) {
        RequestInfo requestInfo = RequestHolder.get();
        RequestInfo.UserInfo invokeUser = requestInfo.getUserInfo();
        RequestInfo.InterfaceInvokeInfo interfaceInfo = requestInfo.getInterfaceInvokeInfo();
        // 判断用户是否有充足的羊毛积分 能够调用该接口
        if(invokeUser.getWoolLeft() < interfaceInfo.getWoolUsed()){
            return AuthenticatorResult.fail("羊毛积分不足");
        }
        return AuthenticatorResult.success();
    }

    @Override
    public int getOrder() {
        return 11;
    }
}
